package com.qp.basics.common.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ImortExportUtil {
    public final static Date date = new Date();
    public final static long localOffset = date.getTimezoneOffset() * 60000;

    public static String toStr(Object o){
        if(o == null)return null;
        return o.toString();
    }
    public static Double toDouble(Object o){
        if(o == null)return null;
        return Double.parseDouble(o.toString());
    }
    public static Float toFloat(Object o){
        if(o == null)return null;
        return Float.parseFloat(o.toString());
    }
    public static Long toLong(Object o){
        if(o == null)return null;
        return Long.parseLong(o.toString());
    }
    public static String optNumberStr(Object o){
        if( o == null)return null;
        Pattern patternx = Pattern.compile("-?[\\.0-9]*");
        Matcher isNumx = patternx.matcher(o.toString());
        if(isNumx.matches())return o.toString();

        Pattern pattern = Pattern.compile("-?\\d+\\.\\d+");
        Matcher matcher = pattern.matcher(o.toString());
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
    public static Double optDouble(Object o){
        if(o == null)return null;
        String str = optNumberStr(o);
        if(str == null || "".equals(str))return null;
        return Double.parseDouble(str);
    }
    public static Integer optInteger(Object o){
        if(o == null)return null;
        String str = optNumberStr(o);
        if(str == null || "".equals(str))return null;
        return Integer.parseInt(str);
    }
    public static Integer toInteger(Object o){
        if(o == null)return null;
        return Integer.parseInt(o.toString());
    }
    public static Date toDate(Object o){
        if(o == null)return null;
        if(o instanceof Date)return (Date)o;
        if(o instanceof Double){
            Date date = new Date();
            date.setTime((long) ((Double.parseDouble(o.toString()) - 25569) * 24 * 3600 * 1000 + localOffset));
            return date;
        }
        String str = o.toString();
        SimpleDateFormat sdf = null;
        if(str.contains(" ") && str.contains(":")) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }else if(str.contains("/")){
            sdf = new SimpleDateFormat("yyyy/MM/dd");
        }else{
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        }
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public static Object camel(Object o){
        if(o instanceof Map){
            Map m = (Map)o;
            Iterator<String> it = m.keySet().iterator();
            Map x = new HashMap();
            while (it.hasNext()){
                String key = it.next();
                String keyx = camelStr(key);
                Object v = m.get(key);
                if(v instanceof Date){
                    v = DateUtil.DateTimeToString((Date)v);
                }
                x.put(keyx,v);
            }
            m.putAll(x);
            return m;
        }
        if(o instanceof List){
           List l = (List)o;
           for (Object x : l){
               camel(x);
           }
           return l;
        }
        return null;
    }

    public static Object deCamel(Object o){
        if(o instanceof Map){
            Map m = (Map)o;
            Iterator<String> it = m.keySet().iterator();
            Map x = new HashMap();
            while (it.hasNext()){
                String key = it.next();
                String keyx = deCamelStr(key);
                Object v = m.get(key);
                if(v instanceof Date){
                    v = DateUtil.DateTimeToString((Date)v);
                }
                x.put(keyx,v);
            }
            m.putAll(x);
            return m;
        }
        if(o instanceof List){
            List l = (List)o;
            for (Object x : l){
                deCamel(x);
            }
            return l;
        }
        return null;
    }
    static String camelStr(String str){
        char[] chars = str.toCharArray();
        char[] charsx = str.toCharArray();
        int cp = 0;
        boolean underline = false;
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if(c == '_'){
                underline = true;
                continue;
            }
            if (underline && c >= 'a' && c <= 'z') {
                c -= 32;
                underline = false;
            }
            charsx[cp++] = c;
        }
        String rtn = new String(charsx,0,cp);
        return rtn;
    }
    static String deCamelStr(String str){
        char[] chars = str.toCharArray();
        char[] charsx = new char[chars.length+5];
        int cp = 0;
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if ( c >= 'A' && c <= 'Z') {
                c += 32;
                charsx[cp++] = '_';
            }
            charsx[cp++] = c;
        }
        String rtn = new String(charsx,0,cp);
        return rtn;
    }

    public static void main(String[] args) {
        String x = camelStr("abc_def_xyz");
        System.out.println(x);
        System.out.println(deCamelStr(x));

//        String str = optNumberStr("所属 30.32");
//        System.out.println(str);
//        str = optNumberStr("所属 -31.32");
//        System.out.println(str);
//        str = optNumberStr("32.32");
//        System.out.println(str);
//        str = optNumberStr("-33.32abc");
//        System.out.println(str);
//        str = optNumberStr("-34");
//        System.out.println(str);
//        str = optNumberStr("35");
//        System.out.println(str);
    }
}
